package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private EmpExprService empExprService;
    @Autowired
    private DeptServiceImpl deptService;


    @Override
    public PageResult<Emp> getPageResule(EmpQueryParam param) {
        Page<Emp> page = new Page<>(param.getPage(), param.getPageSize());
        LambdaQueryWrapper<Emp> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.like(param.getName() != null, Emp::getName, param.getName()).between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd()).eq(param.getGender() != null, Emp::getGender, param.getGender());
        page = page(page, objectLambdaQueryWrapper);
        Set<Long> depts = page.getRecords().stream().map(Emp::getDeptId).collect(Collectors.toSet());
        page.getRecords().forEach(emp -> {
            depts.forEach(deptid -> {
                if (emp.getDeptId() != null && emp.getDeptId().equals(deptid)) {
                    emp.setDeptName(deptService.getById(deptid).getName());
                }
            });
        });


        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    /**
     * 新增员工
     *
     * @param emp
     */
    @Override
    public void saveEmp(Emp emp) {
        save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null) {
            exprList.forEach(s -> s.setEmpId(emp.getId()));
            empExprService.saveBatch(exprList);
        }
    }

    @Override
    public Result getEmpById(Integer id) {
        Emp emp = getById(id);
        Long id1 = emp.getId();
        List<EmpExpr> list = empExprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id1));
        emp.setExprList(list);
        return Result.success(emp);
    }

    @Override
    public void updateEmp(Emp emp) {
        updateById(emp);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null) {
            exprList.forEach(s -> s.setEmpId(emp.getId()));
            empExprService.saveBatch(exprList);
        }
    }

    @Override
    public void deletByIds(List<Integer> ids) {
        removeBatchByIds(ids);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));
    }
}
